﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;

public class ScenesMgr : MonoBehaviour
{
    /// <summary>
    /// 同步加载场景，会等场景加载完成后才能继续后续的流程
    /// </summary>
    /// <param name="name">场景名称</param>
    /// <param name="func">加载后要做的事</param>
    public void LoadScene(string name, UnityAction func)
    {
        //调用unity自带的加载场景api
        SceneManager.LoadScene(name);
        //场景加载完成后执行一些操作
        func();
        
    }
    /// <summary>
    /// 提供给外部的异步加载的接口方法
    /// </summary>
    /// <param name="name"></param>
    /// <param name="func"></param>
    public void LoadSceneAsync(string name, UnityAction func)
    {
        MonoManager.GetInstance().StartCoroutine(ReallyLoadSceneAsync(name,func));
    }
    /// <summary>
    /// 异步加载场景的协程
    /// </summary>
    /// <param name="name"></param>
    /// <param name="func"></param>
    /// <returns></returns>
    private IEnumerator ReallyLoadSceneAsync(string name,UnityAction func)
    {
        AsyncOperation ao = SceneManager.LoadSceneAsync(name);
        
        //可以得到场景加载的一个进度
        //如果异步加载还没有完成
        if (!ao.isDone)
        {
            //通过事件中心向外分发进度情况，外面想用就用
            EventCenter.GetInstance().EventTrigger("进度条更新", ao.progress);
            yield return ao.progress;
        }
        func();
    }
}
